package asia.yuanding.wechat.web.netty.handler;

import cn.hutool.json.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.AttributeKey;

/**
 * @ClassName: MessageHandler
 * @Author: YuanDing
 * @Date: 2025/8/3 21:39
 * @Description:
 */
public interface MessageHandlerUtil {
    String getType();

    void handle(ChannelHandlerContext ctx, JSONObject data);

    static String getUId(ChannelHandlerContext ctx){
     return ctx.channel().attr(AttributeKey.valueOf("uId")) != null ? ctx.channel().attr(AttributeKey.valueOf("uId")).toString() : null;
    }

    static TextWebSocketFrame getResult(Object result) {
        if (result == null) {
            return null;
        }
        return new TextWebSocketFrame(new JSONObject(result).toString());
    }

}
